from django.test import TestCase
from django.contrib.gis.geos import Point
from address.services import AddressService
from address.models import Address
from apps.accounts.models import User

class AddressServiceTest(TestCase):
    def setUp(self):
        self.user = User.objects.create(username='test')
        self.valid_data = {
            'contact': '测试',
            'phone': '+8613812345678',
            'province': '北京',
            'city': '北京',
            'district': '朝阳区',
            'detail': '测试地址',
            'is_default': True,
            'location': Point(116.404, 39.915)
        }

    def test_create_address(self):
        addr = AddressService.create_address(self.user, self.valid_data)
        self.assertEqual(addr.user, self.user)
        self.assertTrue(addr.is_default)
        
        # 测试默认地址唯一性
        with self.assertRaises(Exception):
            AddressService.create_address(self.user, {**self.valid_data, 'contact': '测试2'})